//
//  QPointsVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/3/17.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QPointsVC.h"
#import "QPointsOneCell.h"
#import "QPointsTwoCell.h"
#import "QPointsThreeCell.h"
#import "QPointsDetailCell.h"
#import "QPointsTypeCell.h"
#import "QPointsTipCell.h"
#import "QPointsWebVC.h"
#import "QPointsTwoVC.h"
#import "QPointsNoDataCell.h"
#import "QPointInfoListModel.h"
#import "QPointListModel.h"
#import "QPointDetailModel.h"
#import "QPointSignModel.h"
#import "QShowSignView.h"
const CGFloat headHeight1 = 160;

//webView积分说明
static NSString *Q_BASE_Web = @"http://api.exiaoma.cn/exm_api2.0/html5/pointDesc.jsp";


@interface QPointsVC ()<UITableViewDelegate,UITableViewDataSource,refreshDelegate>
{
    QPointInfoListModel * listModel;
    NSMutableArray * _dataArr;
    NSString * _pointType;
}
@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic, strong) UIImageView *bacImageView;

@end

@implementation QPointsVC

- (id)init{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的积分";
    [self createUI];
    [self requestData];
    [self requestDataWithType:@"2"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeBox) name:kNotificationPointSignSuccess object:nil];
    
}

- (void)closeBox{
    [self requestData];
}

- (void)refreshActionWithType:(NSString *)type{
    _pointType = type;
    [self requestDataWithType:type];
}

- (void)requestDataWithType:(NSString *)type{
    NSString *url = [QParamService urlWithAction:@"getIntegralOrderInfoAction"];
    NSDictionary *para= [QParamService getIntegralOrderInfoActionWithOper_type:type Start:@"1" PageSize:@"5"];
    
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [QViewUtility disMissNoRecordInVC:self];
    [QViewUtility disMissNoNetWorkInVC:self];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.mj_header endRefreshing];
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            if([QUserInfo sharedInfo].isHaveNetWork == NO){
                [QViewUtility showNoNetWorkInView:_tbView];
            }
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [_dataArr removeAllObjects];
            QPointListModel *listModel1 = [QPointListModel objectFromDict:basModel.items];
            [_dataArr addObjectsFromArray:listModel1.list];
            [_tbView reloadData];
            
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}


- (void)requestData{
    
    CLog(@"签到详情");
    
    NSString *url = [QParamService urlWithAction:@"getUserSignInInfoAction"];
    
    NSDictionary *param = [QParamService getUserSignInInfoAction];
    
    [QViewUtility showMBHudToView:self.view hideBg:YES];
    
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        
        if(error){
            return ;
        }
        
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            listModel = [QPointInfoListModel objectFromDict:basModel.items];
            [self.tbView reloadData];
            
        }else{
            
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            if([basModel.errMsg isEqualToString:@"请重新登录"]){
                [QDataUtility deleteLoginModel];
            }
        }
    }];
}

- (void)signData{
    CLog(@"签到");
    //WS(wSelf);
    NSString *url = [QParamService urlWithAction:@"userSignInAction"];
    NSDictionary *param = [QParamService userSignInActionWithAdd_type:@"1"];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            QPointSignModel * model = [QPointSignModel objectFromDict:basModel.items];
            
            [QViewUtility showSignBoxViewWithTitle:[NSString stringWithFormat:@"+%@",model.integrations]];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            if([basModel.errMsg isEqualToString:@"请重新登录"]){
                [QDataUtility deleteLoginModel];
            }
        }
    }];
}

- (void)createUI{
    
    _dataArr = [NSMutableArray array];
    _pointType = @"2";
    _bacImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, kNavHeight+kStatusHeight, kScreenWidth, headHeight1)];
    _bacImageView.image = [UIImage imageNamed:@"mySelfHeadImage1"];
    _bacImageView.userInteractionEnabled = YES;
    [self.view addSubview:_bacImageView];

    
    [self.view addSubview:self.tbView];
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo (@(kNavHeight+kStatusHeight));
        make.left.equalTo (@0);
        make.right.equalTo (@0);
        make.bottom.equalTo (@0);
    }];
    self.tbView.showsVerticalScrollIndicator = NO;
    
    [self.tbView registerNib:[UINib nibWithNibName:@"QPointsOneCell" bundle:nil] forCellReuseIdentifier:@"QPointsOneCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"QPointsTwoCell" bundle:nil] forCellReuseIdentifier:@"QPointsTwoCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"QPointsThreeCell" bundle:nil] forCellReuseIdentifier:@"QPointsThreeCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"QPointsDetailCell" bundle:nil] forCellReuseIdentifier:@"QPointsDetailCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"QPointsTipCell" bundle:nil] forCellReuseIdentifier:@"QPointsTipCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"QPointsTypeCell" bundle:nil] forCellReuseIdentifier:@"QPointsTypeCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"QPointsNoDataCell" bundle:nil] forCellReuseIdentifier:@"QPointsNoDataCell"];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc]init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 2;
    }else if (section == 2){
        return 1;
    }else{
        if (_dataArr.count == 0) {
            return 2;
        }
        return _dataArr.count+1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 160;
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            return 80;
        }else{
            return 100;
        }
    }else{
        if (indexPath.row == 0) {
            return 40;
        }
        if (_dataArr.count == 0) {
            return 200;
        }
        return 50;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WS(wSelf);
    
    if (indexPath.section == 0) {
        QPointsOneCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QPointsOneCell"];
        cell.backgroundColor = [UIColor clearColor];
        if (listModel) {
            cell.model = listModel;
        }
        cell.PointsBlock = ^(){
            QPointsWebVC * web = [[QPointsWebVC alloc]init];
            web.url = Q_BASE_Web;
            [wSelf.navigationController pushViewController:web animated:YES];
        };
        return cell;
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            QPointsTwoCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QPointsTwoCell"];
            if (listModel) {
                cell.model = listModel;
            }
            cell.signBlock = ^(){
                CLog(@"签到");
                __strong QPointsVC *sSelf = wSelf;
                [sSelf signData];
            };
            return cell;

        }else{
            QPointsThreeCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QPointsThreeCell"];
            if (listModel) {
                cell.listModel = listModel;
            }
            return cell;
        }
    }else if (indexPath.section == 2){
        
        QPointsDetailCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QPointsDetailCell"];
        return cell;
    }else{
        if (indexPath.row == 0) {
            QPointsTipCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QPointsTipCell"];
            __weak QPointsTipCell *wCell = cell;
            cell.delegate = self;

            cell.tipBlock = ^(UIButton * sender){

                if (sender.tag == 1) {
                    [wCell.getBtn setTitleColor:[QColorUtility defaultBlueColor] forState:0];
                    [wCell.wasteBtn setTitleColor:[QColorUtility defaultBackColor] forState:0];
                    wCell.lineView.backgroundColor = [QColorUtility defaultBlueColor];
                    wCell.lineView1.backgroundColor = [UIColor clearColor];
                }else{
                    [wCell.wasteBtn setTitleColor:[QColorUtility defaultBlueColor] forState:0];
                    [wCell.getBtn setTitleColor:[QColorUtility defaultBackColor] forState:0];
                    wCell.lineView1.backgroundColor = [QColorUtility defaultBlueColor];
                    wCell.lineView.backgroundColor = [UIColor clearColor];
                }
            };
            return cell;

        }else{
            if (_dataArr.count == 0) {
                QPointsNoDataCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QPointsNoDataCell"];
                cell.backgroundColor = tableView.backgroundColor;
                
                return cell;

            }else{
                QPointsTypeCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QPointsTypeCell"];

                QPointDetailModel * model =[_dataArr objectAtIndex:indexPath.row - 1];
                model.yangkai = _pointType;
                
                model.curr_date = listModel.curr_date;
                cell.model = model;
                
                return cell;
            }
        }
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat offsetY = scrollView.contentOffset.y;
    //NSLog(@">>>>>>>>>%f",offsetY);
    if(offsetY > 0){
        _bacImageView.frame = CGRectMake(0, -offsetY+kNavHeight+kStatusHeight, kScreenWidth, headHeight1);
    }else{
        CGFloat height = ABS(offsetY)+headHeight1;
        CGFloat width = kScreenWidth/headHeight1 * height;
        _bacImageView.frame = CGRectMake(-(width-kScreenWidth)/2, 0+kNavHeight+kStatusHeight, width, height);
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 2) {
        QPointsTwoVC * vc = [[QPointsTwoVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor clearColor];
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
